﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Carbon.Web.Forms;
using Carbon.Web.Forms.Extensions;

namespace Demo.Demos.ComplexDataStructures
{
    public partial class ComplexDataStructures : DemoPage
    {
        public void Calculate_CanDelete(object sender, ConditionEventArgs args)
        {
            ComplexDataStructuresViewModel model = (ComplexDataStructuresViewModel)((Control)sender).GetViewModelContext();
             if (model.SelectedPerson == null)  throw new NullReferenceException("model.SelectedPerson");

            PersonViewModel person = model.SelectedPerson;
            args.Result = person.Parent != null;
        }

        public void Click_Delete(object sender, EventArgs args)
        {
            ComplexDataStructuresViewModel model = (ComplexDataStructuresViewModel)((Control)sender).GetViewModelContext();
             if (model.SelectedPerson == null)  throw new NullReferenceException("model.SelectedPerson");

            PersonViewModel person = model.SelectedPerson;
            person.Delete();

        }

        public void Click_RemoveFriend(object sender, EventArgs args)
        {
            FriendWrapperViewModel model = (FriendWrapperViewModel)((Control)sender).GetViewModelContext();
            model.RemoveFriend();
        }

        public void Click_GoToFriend(object sender, EventArgs args)
        {
            FriendWrapperViewModel model = (FriendWrapperViewModel)((Control)sender).GetViewModelContext();
            model.SelectFriend();
        }

        public String GetImageUrl(object sender)
        {
            ComplexDataStructuresViewModel model = (ComplexDataStructuresViewModel)((Control)sender).GetViewModelContext();
            PersonViewModel person = model.SelectedPerson;

            if (person != null)
            {
                if (person.CustomAvatarPicture != null)
                    return ImageHandler.GetFileReferenceImageUrl(person.CustomAvatarPicture);
                else if (!string.IsNullOrEmpty(person.AvatarPicture))
                    return person.AvatarPicture;
                else
                    return "/Demos/ComplexDataStructures/img/avatar90x90.png";
            }
            else
            {
                return null;
            }
        }

        protected override Carbon.Web.Forms.ViewStatePersistanceMode ViewStatePersistanceMode
        {
            get
            {
                return Carbon.Web.Forms.ViewStatePersistanceMode.Database;
            }
        }       
    }
}